package leetcode;

import java.util.ArrayList;
import java.util.List;

public class SpiralMatrix {

	public static void main(String[] args) {
		int[][] matrix = {{1, 2, 3, 10}, {4, 5, 6, 11}, {7, 8, 9, 12}};
		SpiralMatrix object = new SpiralMatrix();
//		System.out.println(object.spiralOrder(matrix));
		
		int[][] genarete = object.generateMatrix(1);
		for(int i = 0; i < genarete.length; i++){
			for (int j = 0; j < genarete[0].length; j++) {
				System.out.print(genarete[i][j] + "  ");
			}
			System.out.println();
		}
	}
	
	public List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> res = new ArrayList<Integer>();
        if(matrix == null || matrix.length <= 0){
        	return res;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        int rightBoundary = columns;
        int bottomBoundary = rows;
        int topBoundary = 0;
        int leftBoundary = -1;
        
        int curRow = 0;
        int curColumn = columns - 1;
        int length = rows * columns;
        int index = 0;
        while(index < length){
        	//可能在任何时候终止，所以需要在循环内进行判断
        	//右
        	for(int j = leftBoundary + 1; j < rightBoundary; j++){
        		res.add(matrix[curRow][j]);
        		index++;
        	}
        	curRow = bottomBoundary - 1;
        	rightBoundary = rightBoundary - 1;
        	
        	if(index == length){
        		break;
        	}
        	//下
        	for(int i = topBoundary + 1; i < bottomBoundary; i++){
        		res.add(matrix[i][curColumn]);
        		index++;
        	}
        	bottomBoundary -= 1;
        	curColumn = leftBoundary + 1;
        	if(index == length){
        		break;
        	}
        	
        	//左
        	for(int j = rightBoundary - 1; j > leftBoundary; j--){
        		res.add(matrix[curRow][j]);
        		index++;
        	}
        	curRow = topBoundary + 1;
        	leftBoundary += 1;
        	if(index == length){
        		break;
        	}
        	//上
        	for(int i = bottomBoundary - 1; i > topBoundary; i--){
        		res.add(matrix[i][curColumn]);
        		index++;
        	}
        	curColumn = rightBoundary - 1;
        	topBoundary += 1;
        }
        System.out.println(res);
        return res;
    }
	
	
	//从1到n^2 生成螺旋矩阵
	public int[][] generateMatrix(int n) {
        if(n < 0){
        	return null;
        }
        int[][] res = new int[n][n];
        int rightBoundary = n;
        int bottomBoundary = n;
        int topBoundary = 0;
        int leftBoundary = -1;
        
        int curRow = 0;
        int curColumn = n - 1;
        int length = n * n;
        int index = 1;
        while(index <= length){
        	//可能在任何时候终止，所以需要在循环内进行判断
        	//右
        	for(int j = leftBoundary + 1; j < rightBoundary; j++){
        		res[curRow][j] = index++;
        	}
        	curRow = bottomBoundary - 1;
        	rightBoundary = rightBoundary - 1;
        	
        	if(index > length){
        		break;
        	}
        	//下
        	for(int i = topBoundary + 1; i < bottomBoundary; i++){
        		res[i][curColumn] = index++;
        	}
        	bottomBoundary -= 1;
        	curColumn = leftBoundary + 1;
        	if(index > length){
        		break;
        	}
        	
        	//左
        	for(int j = rightBoundary - 1; j > leftBoundary; j--){
        		res[curRow][j] = index++;
        	}
        	curRow = topBoundary + 1;
        	leftBoundary += 1;
        	if(index > length){
        		break;
        	}
        	//上
        	for(int i = bottomBoundary - 1; i > topBoundary; i--){
        		res[i][curColumn] = index++;
        	}
        	curColumn = rightBoundary - 1;
        	topBoundary += 1;
        }
        return res;
    }
}
